package com.chd.driversystem.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chd.driversystem.R;
import com.chd.driversystem.activity.EvaluationActivity;
import com.chd.driversystem.activity.RankingActivity;
import com.chd.driversystem.domain.Ranking;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by feiyu on 17-5-18.
 */

public class RankingAdapter extends RecyclerView.Adapter<RankingAdapter.ViewHolder> {

    private List<Ranking> mRankings;

    private Context mContext;

    public RankingAdapter(Context context, List<Ranking> rankings) {
        mContext = context;
        mRankings = rankings;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_rankingitem, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Ranking ranking = mRankings.get(position);
        if (ranking.getBtAvator() != null) {
            holder.civAvator.setImageBitmap(ranking.getBtAvator());
        } else {
            holder.civAvator.setImageResource(R.drawable.avator);
        }
        String str = "";
        if (position+1 < 10) {
            str = "  ";
        } else if (position+1 < 100) {
            str = " ";
        }
        holder.tvRanking.setText(str + (position+1));
        holder.tvName.setText(ranking.getName());

        str = "";
        if (ranking.getScore() < 10) {
            str = "  ";
        } else if (ranking.getScore() < 100) {
            str = " ";
        }
        holder.tvScore.setText(str + ranking.getScore());
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EvaluationActivity.actionStart(mContext,ranking.getUserId(),((RankingActivity)mContext).getRankingType());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mRankings.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout item;
        CircleImageView civAvator;
        TextView tvRanking;
        TextView tvName;
        TextView tvScore;

        public ViewHolder(View itemView) {
            super(itemView);
            item = (RelativeLayout) itemView;
            civAvator = (CircleImageView) itemView.findViewById(R.id.civ_avator);
            tvRanking = (TextView) itemView.findViewById(R.id.tv_ranking);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvScore = (TextView) itemView.findViewById(R.id.tv_score);
        }
    }
}
